package cn.mesmile.shiro.module.jwt;

import cn.mesmile.shiro.utils.JwtUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;

/**
 * @author zb
 * @date 2021/2/21 13:13
 * @Description:
 */
@Slf4j
public class JwtCredentialsMatcher implements CredentialsMatcher {


    /**
     * JwtCredentialsMatcher只需验证JwtToken内容是否合法
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
        // token
        String token = authenticationToken.getCredentials().toString();
        // 身份
        Object principal = authenticationToken.getPrincipal();
        try {
            System.out.println(">>>>>>>>>>>>>>>>>>> 校验 token 是否合法 jwt");
            // 验证token 是否合法
            return JwtUtil.verify(token);
        } catch (JWTVerificationException e) {
            log.error(e.getMessage());
        }
        return false;
    }
}
